<template>
    <div>
        <el-upload
            class="upload-demo"
            drag
            action=""
            :limit="limit"
            ref="upload"
            :on-change="handleBeforeUpload"
            :on-remove="handleRemove"
            :on-exceed="handleExceed"
            :auto-upload="false"
            :file-list="fileList"
            list-type="picture"
            accept="image/jpg,image/jpeg,image/png"
            multiple>
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过{{ fileSize }}MB</div>
            </el-upload>
            <!-- 上传提示 -->
            <div class="el-upload__tip" slot="tip" v-if="showTip">
            请上传
                <template v-if="fileSize"> 大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b> </template>
                <template v-if="fileType"> 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b> </template>
            的文件
            </div>
    </div>
</template>
<script>
export default {
    data(){
        return{
            files: [], // 复刻文件数据
            fileList:[],
        }
    },
    props: {
        value: [String, Object, Array],
        // 图片数量限制
        limit: {
            type: Number,
            default: 5,
        },
        // 大小限制(MB)
        fileSize: {
            type: Number,
            default: 5,
        },
        // 文件类型, 例如['png', 'jpg', 'jpeg']
        fileType: {
            type: Array,
            default: () => ["png", "jpg", "jpeg"],
        },
    },
    computed: {
        // 是否显示提示
        showTip() {
            return this.isShowTip && (this.fileType || this.fileSize);
        }
    },
    methods: {
    // 上传前loading加载
    handleBeforeUpload(file,fileList) {
        console.log('handleBeforeUpload',file,fileList)
      let isImg = false;
      if (this.fileType.length) {
        let fileExtension = "";
        if (file.raw.name.lastIndexOf(".") > -1) {
          fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1);
        }
        isImg = this.fileType.some(type => {
          if (file.raw.type.indexOf(type) > -1) return true;
          if (fileExtension && fileExtension.indexOf(type) > -1) return true;
          return false;
        });
      } else {
        isImg = file.raw.type.indexOf("image") > -1;
      }

      if (!isImg) {
        this.$modal.msgError(`文件格式不正确, 请上传${this.fileType.join("/")}图片格式文件!`);
        return false;
      }
      if (this.fileSize) {
        const isLt = file.size / 1024 / 1024 < this.fileSize;
        if (!isLt) {
          this.$modal.msgError(`上传头像图片大小不能超过 ${this.fileSize} MB!`);
          return false;
        }
      }
       // 复刻文件信息
      this.files = file.raw
      this.allMall(fileList)
    },
    handleRemove(file,fileList){
        this.allMall(fileList)
    },
    allMall(fileList){
        this.fileList=fileList
        console.log(this.fileList)
        this.$emit('getFileList',this.fileList)
    },
    // 文件个数超出
    handleExceed() {
      this.$modal.msgError(`上传文件数量不能超过 ${this.limit} 个!`);
    },
    uploadImage(ops) {

    },
    clearImage(){
        this.$refs.upload.clearFiles()
    }
    },
}
</script>